﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Base;


namespace HR60_PanelSoft.View.TabForms.ListItems
{
    //GC: [g1]修正内存对象不能加载[SceneAttrList] @s1
    //GC: [g2]场景保存，高级设备会重复bug[SceneAttrList]
    //GC: [g3]bug2610[SceneAttrList]
	public partial class SceneAttrList : BaseFormControl
	{
		private Scene scene = null;

		private Device device = null;
		
		public SceneAttrList()
		{
			InitializeComponent();
		}

		// 目标场景
		public Scene TagScene
		{
			get
			{
				return scene;
			}
		}

		// 目标设备
		public Device TagDevice
		{
			get
			{
				return device;
			}
		}

		// 初始化界面
		public void init(Device device, Scene scene)
		{
			this.device = device;
			this.scene = scene;
			SceneEvent se = scene.findEventByDevice(device.uuid);

			ltlName.Text = device.nickName;
			tblList.RowCount = device.type.attrs.Count + 1;

			foreach (CtrlAttr attr in device.type.attrs)
			{
				SceneAttrItem item = new SceneAttrItem(attr);
				item.AutoSubmitFlag = false;
				tblList.Controls.Add(item);
				if (se != null && se.attrs.IndexOf(attr.name) > -1)
				{
                    /* [g1.s1] */
                    //item.Value = se.attrValueKeys[se.attrs.IndexOf(attr.name)];

                    item.LoadValue(se);
                    
                    /* end of [g1.s1] */
				}

                /* [g2.s2]调整事件委托方式 */
                //item.ValueChanged += new EventHandler(valueChange);
                //item.ValueChanged += new EventHandler(Item_ValueChanged);
                item.ValueChanged += new ValueChangedHandler(Item_ValueChanged);

                /* end of [g2.s2] */
                ////GC:
                //item.AutoSubmitFlag = true;
			}
		}

		// 重写Value属性，返回SceneEvent对象，只读
		public new object Value
		{
			get
			{
				SceneEvent se = new SceneEvent();
				se.device = device.uuid;
				for (int i = 1; i < tblList.Controls.Count; i++)
				{
					SceneAttrItem item = (SceneAttrItem) tblList.Controls[i];
					string valueKey = (string)item.Value;
					if (valueKey != null)
					{
						se.attrs.Add(item.TagAttr.name);
						se.attrValueKeys.Add(valueKey);
					}
				}
				return se.attrs.Count == 0 ? null : se;
			}
		}

        /* [g2.s1] */
        //public void Item_ValueChanged(object sender, EventArgs ea)
        public void Item_ValueChanged(object sender, ValueChangedEventArgs ea)
        {
            bool hasAttrs = false;
            /* 遍历是否有勾选 */
            foreach (Control c in tblList.Controls)
            {
                if (!(c is SceneAttrItem))
                    continue;
                SceneAttrItem sai = (SceneAttrItem)c;
                if (sai.Value != null)
                {
                    hasAttrs = true;
                    break;
                }
            }
            
            if (hasAttrs) /* 如果有勾选*/
            {
                SceneEvent se = scene.findEventByDevice(device.uuid);


                if (se != null) /* 如果存在，先删除 */
                {

                    for (int i = scene.events.Count - 1; i >= 0; i--)
                    {
                        if (scene.events[i].device == device.uuid)
                            scene.events.RemoveAt(i);
                    }
                }
                SceneEvent newse = new SceneEvent();
                newse.device = device.uuid;
                for (int i = 1; i < tblList.Controls.Count; i++)
                {
                    SceneAttrItem item = (SceneAttrItem)tblList.Controls[i];
                    string valueKey = (string)item.Value;
                    if (valueKey != null)
                    {
                        newse.attrs.Add(item.TagAttr.name);
                        newse.attrValueKeys.Add(valueKey);
                    }
                }
                scene.events.Add(newse);
            }
            else /* 如果无勾选，则删除该设备相关 */
            {
                /* [g3.s1]bug2910 */
                //foreach (SceneEvent s in scene.events)
                //{
                //    if (s.device == device.uuid)
                //        scene.events.Remove(s);
                //}
                List<int> tempIdx = new List<int>();
                foreach (SceneEvent s in scene.events)
                {
                    if (s.device == device.uuid)
                        tempIdx.Add(scene.events.IndexOf(s));
                }
                tempIdx.OrderByDescending(x => x);

                foreach (int i in tempIdx)
                {
                    scene.events.RemoveAt(i);
                }
                /* end of[g3.s1] */
            }

            /* 提交 */
            //
            valueChange(sender, ea);
        }
        /* end of [g2.s1] */

	}
}
